package EditorImagenes;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JOptionPane;

public class Celdas extends java.awt.Component implements Serializable
{
    Color general=null;
    public void setColor(Color color){
    general=color;
    }
    public static int getColor() {
        return color;
    }

    public static void setColor(int color) {
        Celdas.color = color;
    }
    public static final int SET_BLOCKS=0,SET_START=1,SET_FINISH=2;
    public static final double NORMAL = 1,EASY = 0.3,TOUGH =5,VERY_TOUGH=10,BLOCK=Double.MAX_VALUE;
    private static double newBlockStrength = BLOCK;
    private  int editMode = SET_BLOCKS;
    private  Celdas startCell;
    private static Celdas finishCell;
    private static int color=0;

  
    private boolean isStart = false;
    private boolean isFinish = false;
    
    private static Vector cells = new Vector();
    
    public static boolean tidy = false;
    
    private static boolean showPath = true;
    private static boolean showDist = true;
    
    private double cost = 1.0;
    private transient boolean used = false;
    private transient double distFromStart = -1;
    private transient double distFromFinish = -1;
   // private boolean totalBlock = false;
    
    private boolean partOfPath = false;
    
    private Point position;
    
    public Celdas(){
        cells.addElement(this);
        tidy=true;
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }
    
    
    public Celdas(boolean block){
        this();
        setTotalBlock(block);
    }
    
    public void setPosition(Point p){
        position = p;
    }
    
    public Point getPosition(){
        return position;
    }
      
    public  void setEditMode(int mode){
        editMode = mode;
        
    }    
        
    public void processMouseEvent(MouseEvent e){
        super.processMouseEvent(e);
        if(e.getID()==e.MOUSE_CLICKED){
            if (e.getClickCount()== 2){
        
        
       // JOptionPane.showMessageDialog(null, "F()= "+ver+"  "+cost +" =  " +distFromFinish );
        
                Color a=Color.WHITE;
                
       }
            setShowPath(false);
            switch(editMode){
                case(SET_BLOCKS):
                    if(cost!=newBlockStrength){cost=newBlockStrength;}
                    else{cost=NORMAL;}
                    repaint();
                    break;
                case(SET_START):
                    setStart(true);
                    break;
                case(SET_FINISH):
                    setFinish(true);
                    break;
                
            }
        }
        
    }
    public void TeAmoMydelin(){
          setShowPath(false);
            switch(editMode){
                case(SET_BLOCKS):
                    if(cost!=newBlockStrength){cost=newBlockStrength;}
                    else{cost=NORMAL;}
                    repaint();
                    break;
                case(SET_START):
                    setStart(true);
                    break;
                case(SET_FINISH):
                    setFinish(true);
                    break;
                
            }
        
    }
    public Dimension getPreferredSize(){
        return new Dimension(10,10);
    }
 double ver=0;
    public void addToPathFromStart(double distSoFar){
        used = true;
        ver=distSoFar;
        if(distFromStart == -1){
            distFromStart = distSoFar+cost;
            return;
        }
        if(distSoFar+cost<distFromStart){
            distFromStart = distSoFar+cost;
        }
    }
    
    public void addToPathFromFinish(double distSoFar){
        used = true;
        ver=distSoFar;
        if(distFromFinish == -1){
            distFromFinish = distSoFar+cost;
            return;
        }
        if(distSoFar+cost<distFromFinish){
            distFromFinish = distSoFar+cost;
        }
    }
        
    
    public double getCost(){
        return cost;
    }
    
    
    public void setCost(double c){
        cost=c;
    }
    
    public  Celdas getStartCell(){
        return startCell;
    }
    
    public boolean isStart(){
        return startCell == this;
    }
    
    public void setStart(boolean flag){
            if(flag){
                Celdas temp = this;
                
                startCell=this;
                isStart=true;
                repaint();
                temp.repaint();
            }
            else{
                isStart=false;
            }
               
    }
    
     public static Celdas getFinishCell(){
        return finishCell;
    }
    
    public boolean isFinish(){
        return finishCell == this;
    }
    
    public void setFinish(boolean flag){
        if(flag){
            Celdas temp = this;
            if(finishCell!=null){temp=finishCell;temp.setFinish(false);}
            finishCell=this;
            isFinish=true;
            repaint();
            temp.repaint();
        }
        else{
                isFinish=false;
            }
    }
    
    public boolean isTotalBlock(){
        return cost==BLOCK;
    }
    
    
    
    public void setTotalBlock(boolean flag){
        if(flag){cost = BLOCK;}
        else{cost = NORMAL;}
    }
    
    public boolean isUsed(){
        return used;
    }
    
    private void resetCell(){
        used = false;
        setPartOfPath(false);
        distFromStart = distFromFinish = -1;
    }
    
    public static void reset(){
        for(int i=0;i<cells.size();i++){
            ((Celdas)cells.elementAt(i)).resetCell();
        }
    }
    
    private void clearCell(){
        setCost(NORMAL);
    }
    
   public static void clearAll(){
        for(int i=0;i<cells.size();i++){
            ((Celdas)cells.elementAt(i)).clearCell();
        }
    }
        
    
    public static void setNewBlockStrength(double s){
        if(s<0){newBlockStrength = BLOCK;}
        else{newBlockStrength = s;}
    }
    
    public static void setShowPath(boolean flag){
            showPath = flag;
    }
    
    public static boolean isShowPath(){
        return showPath;
    }
    
    public boolean isPartOfPath(){
        return partOfPath;
    }
    
    public void setPartOfPath(boolean flag){
        partOfPath = flag;
    }
    
    
   
    public double getDistFromStart(){
        if(this.startCell == this){return 0;}
        if(isTotalBlock()){return -1;}
        return distFromStart;
    }
    public double getDistFromFinish(){
    if(Celdas.finishCell==this){return 0;}
    if(isTotalBlock()){return -1;}
    return distFromFinish;
    }
    
    public void paint(Graphics g){
        Dimension size = getSize();
        g.setColor(Color.white); 
        if(cost!=NORMAL){
            if(cost==EASY){g.setColor(Color.orange);}
            if(cost==BLOCK){
                
        switch(Celdas.getColor()){
            case 1:
                g.setColor(Color.BLUE);
            
            case 2:
                g.setColor(Color.RED);
                        }
                
            
            }
            
            if(cost==TOUGH){g.setColor(Color.GREEN);}
            if(cost==VERY_TOUGH){g.setColor(Color.BLUE);}
        }
        if(showPath && partOfPath){
            g.setColor(Color.RED);
        }
        if(startCell == this){
            g.setColor(general);
        }
        if(finishCell == this){
            g.setColor(Color.YELLOW);
        }
        g.fillRect(0,0,size.width,size.height);
                
            
        g.setColor(Color.black);
        
        
            
        
        if(showDist && distFromStart>0){g.drawString(""+distFromStart,1,(int)(size.height*0.75));}
        g.drawRect(0,0,size.width-1,size.height-1);    
            
    }
    
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
        if(!tidy){
            
            cells = new Vector();
            tidy=true;
        }
        
        ois.defaultReadObject();
        cells.addElement(this);
        if(isStart){setStart(true);}
        if(isFinish){setFinish(true);}
       
        
    }
        
        
    
}